var PR_ADD = {};


PR_ADD.init = function() {
//	$("#btnSave").show();
//	$("#btnUpdate").hide();
};


PR_ADD.loadDataTable = function(isSearch) {
    
    var url = "../pr/searchDetail";
    
    var columns =  [ {
	    "mData" : null,
	    "sClass" : "center"
	}, {
	    "mData" : null,
	    "sClass" : "center"
	}, {
	    "mData" : "requestNo",
	    "sClass" : "center"
	}, {
	    "mData" : "deptRequest",
	    "sClass" : "center"
	}, {
	    "mData" : "projectId",
	    "sClass" : "left"
	}, {
	    "mData" : "institution",
	    "sClass" : "left"
	}, {
	    "mData" : "projectNickname",
	    "sClass" : "left"
	}, {
	    "mData" : "requestType",
	    "sClass" : "left"
	}, {
	    "mData" : "jobRequireDateStr",
	    "sClass" : "center"
	}, {
	    "mData" : "deliveryDateStr",
	    "sClass" : "center"
	}, {
	    "mData" : "prNo",
	    "sClass" : "left"
	}, {
	    "mData" : "prDateStr",
	    "sClass" : "center"
	} ];

    $('#dataTable').dataTable(
		    {
			"bDestroy" : true,
			"bProcessing" : true,
			"bServerSide" : true,
			"bLengthChange" : false,
			"bFilter" : false,
			"bSort" : false,
			"bInfo" : true,
			"bAutoWidth" : false,
			"bJQueryUI" : false,
			"iDisplayLength" : 10,
			"iSortingCols" : 0,
			"sPaginationType" : "bootstrap",
			"sAjaxSource" : url ,
			"sCharSet": "utf-8" ,
			"fnServerParams" : function(aoData) {
			    if(isSearch){
    			    	aoData.push({
    					"name" : "purchaseFormModel.requestNo",
    					"value" :  $('#requestNo').val()
    				    },{
    					"name" : "purchaseFormModel.projectId",
    					"value" :  $('#projectId').val()
    				    },{
    					"name" : "purchaseFormModel.institution",
    					"value" :  $('#institution').val()
    				    },{
    					"name" : "purchaseFormModel.projectNickname",
    					"value" :  $('#projectNickname').val()
    				    });
			    };
			},
			"fnRowCallback" : function(nRow, aData, iDisplayIndex) {
    			    $('td:eq(0)', nRow).html(
    				    '<input type="checkbox" name="keyDeletes" id="keyDeletes" value="'
				    + aData.requestNo + '" />');
			    $('td:eq(1)', nRow).html(iDisplayIndex+1);
			    $('td:eq(2)', nRow).html(
				    '<a href="javascript:PR_HOME.getById('
					    + aData.requestNo + ');">'
					    + aData.requestNo + '</a>');
    			    
    			    $('td:eq(7)', nRow).html(PR_HOME.requestType[aData.requestType]);
			},
			"aoColumns" : columns
		    });
};


PR_ADD.getByRequestNo = function(reqNo) {
    
    var param = {
	    'purchaseHeaderModel.requestPurchaseFormRequestNo' : reqNo
    };
    
    $.getJSON('searchHeader', param ,function(data) {
	PR_ADD.jsonFormData = data ;
	PR_ADD.loadFormData(data);
    });

};

PR_ADD.loadFormData = function(jsonData) {
    
    var headerData = jsonData['purchaseHeaderModel'];
    for (var item in headerData) {
//        $('input[name="'+'purchaseHeaderModel.'+item+'"]').val(headerData[item]);
	
	if(item.trim() == 'typeCost'){
	    $("#typecost_"+headerData[item]).prop('checked', true);
//	    if(headerData[item] == '1'){
//		$("#typecost_1").prop('checked', true);
//		console.log(item+'1');
//	    }else if (headerData[item] == '2'){
//		console.log(item+'2');
//		$("#typecost_1").prop('checked', true);
//	    }
	}
//	$(":checkbox[name='notifyday'][value='wed']").attr('checked', true);
//	console.log(item);
    }
};

PR_ADD.getURLParameter = function(name) {
    var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
};

$(document).ready(function() {
    
    
//    PR_ADD.loadDataTable();
    $('#example').dataTable({
    	"bProcessing" : false,
    	"bServerSide" : false,
    	"bLengthChange" : false,
    	"bFilter" : false,
    	"bSort" : false,
    	"bInfo" : false,
    	"bAutoWidth" : false,
    	"bJQueryUI" : false,
    	"iDisplayLength" : 10,
    	"iSortingCols" : 0,
    	"bPaginate" : false,
    	"sPaginationType" : "bootstrap"
    });

//    $("#btnSearch").click(function() {
//    	$('.required').unValidate();
////    	PR_ADD.loadDataTable(true);
//    });
    
//    $("#btnClear").click(function() {
////	PR_ADD.clearHeaderCriteria();
//    });
    if(PR_ADD.getURLParameter('requestNo')){
	PR_ADD.getByRequestNo(PR_ADD.getURLParameter('requestNo'))
    }
    
     
});
